<?php
/**
 * communit.as
 * @copyright (C)2008 Jaybill McCarthy, All Rights Reserved.
 * @category communitas
 * @package communitas
 * @author Jaybill McCarthy
 * @link http://communit.as communit.as
 * @license http://communit.as/docs/license License
 */

/**
 *
 * @package communitas
 * @subpackage forum_lib
 * @license http://communit.as/docs/license License 
 */

class FilterBBCode implements Zend_Filter_Interface{
	
	function FilterBBCode(){		
		$this->_bbcode = new StringParser_BBcode();
		$this->_bbcode->addFilter (STRINGPARSER_FILTER_PRE, 'Cts_Common::convertlinebreaks');
		
		$this->_bbcode->addParser (array ('block', 'inline', 'link', 'listitem'), 'htmlspecialchars');
		$this->_bbcode->addParser (array ('block', 'inline', 'link', 'listitem'), 'nl2br');
		$this->_bbcode->addParser ('list', 'FilterBBCode::bbcode_stripcontents');
		
		$this->_bbcode->addCode ('quote', 'usecontent?', 'FilterBBCode::do_bbcode_quote', array ('usecontent_param' => 'default'),
		                  'inline', array ('block','inline'), array ('link'));

		$this->_bbcode->addCode ('b', 'simple_replace', null, array ('start_tag' => '<b>', 'end_tag' => '</b>'),
		                  'inline', array ('listitem', 'block', 'inline', 'link'), array ());
		$this->_bbcode->addCode ('i', 'simple_replace', null, array ('start_tag' => '<i>', 'end_tag' => '</i>'),
		                  'inline', array ('listitem', 'block', 'inline', 'link'), array ());
		
		
		$this->_bbcode->addCode ('url', 'usecontent?', 'FilterBBCode::do_bbcode_url', array ('usecontent_param' => 'default'),
		                  'link', array ('listitem', 'block', 'inline'), array ('link'));
		$this->_bbcode->addCode ('link', 'callback_replace_single', 'FilterBBCode::do_bbcode_url', array (),
		                  'link', array ('listitem', 'block', 'inline'), array ('link'));
		$this->_bbcode->addCode ('img', 'usecontent', 'FilterBBCode::do_bbcode_img', array (),
		                  'image', array ('listitem', 'block', 'inline', 'link'), array ());
		$this->_bbcode->addCode ('bild', 'usecontent', 'FilterBBCode::do_bbcode_img', array (),
		                  'image', array ('listitem', 'block', 'inline', 'link'), array ());
		$this->_bbcode->setOccurrenceType ('img', 'image');
		$this->_bbcode->setOccurrenceType ('bild', 'image');
		$this->_bbcode->setMaxOccurrences ('image', 2);
		$this->_bbcode->addCode ('list', 'simple_replace', null, array ('start_tag' => '<ul>', 'end_tag' => '</ul>'),
		                  'list', array ('block', 'listitem'), array ());
		$this->_bbcode->addCode ('*', 'simple_replace', null, array ('start_tag' => '<li>', 'end_tag' => '</li>'),
		                  'listitem', array ('list'), array ());
		$this->_bbcode->setCodeFlag ('*', 'closetag', BBCODE_CLOSETAG_OPTIONAL);
		$this->_bbcode->setCodeFlag ('*', 'paragraphs', true);
		$this->_bbcode->setCodeFlag ('list', 'paragraph_type', BBCODE_PARAGRAPH_BLOCK_ELEMENT);
		$this->_bbcode->setCodeFlag ('list', 'opentag.before.newline', BBCODE_NEWLINE_DROP);
		$this->_bbcode->setCodeFlag ('list', 'closetag.before.newline', BBCODE_NEWLINE_DROP);
		$this->_bbcode->setRootParagraphHandling (false);
        
        
	}
	
	
	// Remove everything but the newline charachter
	static function bbcode_stripcontents($text) {
	    return preg_replace("/[^\n]/", '', trim($text));
	}
	
	static function do_bbcode_quote($action, $attributes, $content, $params, $node_object) {
		
		//dd(array($action, $attributes, $content, $params, $node_object));	
	
	    if (!isset ($attributes['default'])) {
	        $url = $content;
	        $text = htmlspecialchars ($content);
	    } else {
	        $url = $attributes['default'];
	        $text = $content;
	    }
	    if ($action == 'validate') {
	        if (substr ($url, 0, 5) == 'data:' || substr ($url, 0, 5) == 'file:'
	          || substr ($url, 0, 11) == 'javascript:' || substr ($url, 0, 4) == 'jar:') {
	            return false;
	        }
	        return true;
	    }
	    
	   $outstr = "<blockquote><p class=\"author\"><a href=\"/profile/".$attributes['default'].
			"\"><cite>".$attributes['default']."</cite></a> said:</p>" .
	   		"<p>".$text."</p></blockquote>";
	    
	    return $outstr;
	    
	}	
	
	static function do_bbcode_url($action, $attributes, $content, $params, $node_object) {
	    if (!isset ($attributes['default'])) {
	        $url = $content;
	        $text = htmlspecialchars ($content);
	    } else {
	        $url = $attributes['default'];
	        $text = $content;
	    }
	    if ($action == 'validate') {
	        if (substr ($url, 0, 5) == 'data:' || substr ($url, 0, 5) == 'file:'
	          || substr ($url, 0, 11) == 'javascript:' || substr ($url, 0, 4) == 'jar:') {
	            return false;
	        }
	        return true;
	    }
	    return '<a href="'.htmlspecialchars($url).'">'.$text.'</a>';
	}
	
	// Function to include images
	static function do_bbcode_img($action, $attributes, $content, $params, $node_object) {
	    if ($action == 'validate') {
	        if (substr ($content, 0, 5) == 'data:' || substr ($content, 0, 5) == 'file:'
	          || substr ($content, 0, 11) == 'javascript:' || substr ($content, 0, 4) == 'jar:') {
	            return false;
	        }
	        return true;
	    }
	    return '<img class="forum-user-image" src="'.htmlspecialchars($content).'" alt="">';
	}
	
	
	function filter($instr){
		$outstr = @$this->_bbcode->parse($instr);		
		return $outstr;	
	}
	
}
